//
//  CellForScroller.m
//  MY Project For Sport
//
//  Created by dllo on 16/1/25.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "CellForScroller.h"
#import "ModelForHotScroller.h"
#import <SDWebImage/UIImageView+WebCache.h>

#define WEIGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667


@interface CellForScroller ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *scroller;
@property (nonatomic, retain) UIImageView *ImageForScroll;
@property (nonatomic, retain) UILabel *labelForScroller;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) UIPageControl *page;
@end

@implementation CellForScroller
- (void)dealloc {

    [_scroller release];
    [_ImageForScroll release];
    [_array release];
    [_labelForScroller release];
    [_page release];
    [super dealloc];



}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        [self createSubview];
        
    }

    return self;
}


/** 创建子视图 */
- (void)createSubview {
    
// 设置 scroller 的相关属性
    self.scroller = [[UIScrollView alloc] initWithFrame:self.contentView.frame];
    self.scroller.layer.cornerRadius = 8;
    self.scroller.layer.masksToBounds = YES;
    [self.contentView addSubview:self.scroller];
    [_scroller release];
    
    
    self.scroller.backgroundColor = [UIColor yellowColor];
    self.scroller.delegate = self;
    self.scroller.bounces = NO;
    self.scroller.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * 8, self.contentView.frame.size.height);
    self.scroller.pagingEnabled = YES;
    
    // 设置 label的相关属性
    
    self.labelForScroller= [[UILabel alloc] init];
    
    [self.contentView addSubview:self.labelForScroller];
    self.labelForScroller.font = [UIFont systemFontOfSize:18];
    
    [_labelForScroller release];
    
    self.labelForScroller.textColor = [UIColor whiteColor];

    
    self.page = [[UIPageControl alloc] init];
    [self.contentView addSubview:self.page];
    [_page release];
    
    // 自动轮播图 通过 nstimer 实现
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];

}


// 通过请一个界面传过来的 array 对轮播图赋值
-(void)passModelForHotScroller:(NSMutableArray *)array {

    self.page.numberOfPages = array.count;
    
    self.array = [NSMutableArray array];
    
    if (array.count == array.count) {
        
        [array addObject:[array firstObject]];
        [array insertObject:[array objectAtIndex:array.count-2] atIndex:0];

        
    }
    
    self.scroller.contentOffset = CGPointMake([UIScreen mainScreen].bounds.size.width, 0);
    
// 每页轮播图的 title
    for (int i = 0; i < array.count; i++) {
        
        if (i == 1) {
            
            ModelForHotScroller *model = array[i];
            self.labelForScroller.text = model.title;
            
        }
        
    }
    
       self.array = [NSMutableArray arrayWithArray:array];
}

/**当轮播图减速完成时候调用此协议方法, 实现轮播效果*/

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    for (int i = 0; i < self.array.count; i++) {
        
        if (i * scrollView.frame.size.width == scrollView.contentOffset.x) {
            ModelForHotScroller *model = self.array[i];
            self.labelForScroller.text = model.title;
        }
        
    }
    
    if (self.scroller.contentOffset.x == 0) {
        
        self.scroller.contentOffset = CGPointMake(self.ImageForScroll.frame.size.width * (self.array.count - 2), 0);
        self.page.currentPage = self.array.count - 2;
        
    } if (self.scroller.contentOffset.x == self.ImageForScroll.frame.size.width * (self.array.count - 1)) {
        
        self.scroller.contentOffset = CGPointMake(self.ImageForScroll.frame.size.width , 0);
        
        self.page.currentPage = 0;
    }
    
    
}

- (void)layoutSubviews {

    [super layoutSubviews];
    
    self.scroller.frame = self.contentView.frame;
    
    for (int i = 0; i < self.array.count; i++) {
        
    // 每页轮播图的 image
    self.ImageForScroll = [[UIImageView alloc] initWithFrame:CGRectMake(i * [UIScreen mainScreen].bounds.size.width, 0, [UIScreen mainScreen].bounds.size.width, self.contentView.frame.size.height)];
        
        ModelForHotScroller *model2 = self.array[i];
        
    // 给每个 image 赋值
        [self.ImageForScroll sd_setImageWithURL:[NSURL URLWithString:model2.image] placeholderImage:[UIImage imageNamed:@"haha.jpg"]];
        [self.scroller addSubview:self.ImageForScroll];
         [_ImageForScroll release];

    }

    self.labelForScroller.frame = CGRectMake(8 * WEIGHTT, self.contentView.frame.size.height / 5 * 4, self.contentView.frame.size.width - 70 * WEIGHTT, 20 * HEIGHTT);

    
    self.page.frame = CGRectMake(self.contentView.frame.size.width / 3, self.contentView.frame.size.height / 5 * 4 + 15, self.contentView.frame.size.width / 3 * 2, 20 * HEIGHTT);
    
}


/** 自动轮播图 */
- (void)timeAction {
    
    
    self.scroller.contentOffset = CGPointMake(self.ImageForScroll.frame.size.width + self.scroller.contentOffset.x, 0);
    self.page.currentPage = self.scroller.contentOffset.x / self.ImageForScroll.frame.size.width - 1;
    // 轮播图每页的 title与轮播图每页的图片相匹配
    for (int i = 0; i < self.array.count; i++) {
        
        if (i * self.scroller.frame.size.width == self.scroller.contentOffset.x) {
            ModelForHotScroller *model = self.array[i];
            self.labelForScroller.text = model.title;

        }
        
    }

    if (self.scroller.contentOffset.x == 0) {
        
        self.scroller.contentOffset = CGPointMake(self.ImageForScroll.frame.size.width * (self.array.count - 2), 0);
         self.page.currentPage = self.array.count - 2;
        
        
    } if (self.scroller.contentOffset.x == self.ImageForScroll.frame.size.width * (self.array.count - 1)) {
        
        self.scroller.contentOffset = CGPointMake(self.ImageForScroll.frame.size.width , 0);
        
        self.page.currentPage = 0;

    }
    
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
